package com.pm.pmapi.common.api;

import com.github.pagehelper.PageInfo;
import com.pm.pmapi.common.utils.PageUtil;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * @author Jerry Zhang <https://github.com/doughit>
 * @Description 实用PageHelper进行通用数据分页封装
 * @Copyright DoughIt Studio - Powered By DoughIt
 * @date 2021-10-12 09:37
 */
public class CommonPage<T> {
    /**
     * 当前页码
     */
    private Integer pageNum;
    /**
     * 每页数量
     */
    private Integer pageSize;
    /**
     * 总页数
     */
    private Integer totalPage;
    /**
     * 总条数
     */
    private Long total;
    /**
     * 分页数据
     */
    private List<T> list;

    /**
     * 将PageHelper分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(List<T> list) {
        CommonPage<T> result = new CommonPage<T>();
        PageInfo<T> pageInfo = new PageInfo<T>(list);
        result.setTotalPage(pageInfo.getPages());
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 从pageList获取pageInfo信息，返回resList数据
     * @param pageList
     * @param resList
     * @param <T>
     * @return
     */
    public static <T> CommonPage<T> restPage(List<T> pageList, List<T> resList) {
        if (pageList == null || resList == null) {
            return null;
        }
        if (pageList.size() != resList.size()) {
            return null;
        }
        CommonPage<T> result = restPage(pageList);
        result.setList(resList);
        return result;
    }

    /**
     * 将SpringData分页后的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(Page<T> pageInfo) {
        CommonPage<T> result = new CommonPage<T>();
        result.setTotalPage(pageInfo.getTotalPages());
        result.setPageNum(pageInfo.getNumber());
        result.setPageSize(pageInfo.getSize());
        result.setTotal(pageInfo.getTotalElements());
        result.setList(pageInfo.getContent());
        return result;
    }

    /**
     * 将未分页的list转为分页信息
     */
    public static <T> CommonPage<T> restPage(List<T> list, Integer pageNum, Integer pageSize) {
        CommonPage<T> result = new CommonPage<T>();
        if (pageNum == 0) {
            result.setList(list);
            result.setTotal((long) list.size());
            result.setTotalPage(1);
            result.setPageSize(list.size());
            result.setPageNum(1);
        } else {
            result.setList(PageUtil.startPage(list, pageNum, pageSize));
            result.setTotal((long) list.size());
            if (list.size() % pageSize != 0)
                result.setTotalPage(list.size() / pageSize + 1);
            else result.setTotalPage(list.size() / pageSize);
            result.setPageSize(pageSize);
            result.setPageNum(pageNum);
        }

        return result;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }
}
